﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.Restaurant;

namespace BrnMall.Manager.Manager.Restaurant
{
    /// <summary>
    /// 餐桌管理
    /// </summary>
    public static class DiningTableManager
    {
        public static void AddTableType(DiningTableType tableType)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(tableType);
            }
        }
        public static IList<DiningTable> GetAllDiningtable(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<DiningTable>("select * from bma_diningtable where storeid=@0", storeId);
            }
        }

        public static void EditTableType(DiningTableType tableType)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(tableType);
            }
        }

        public static void DeleteTableType(int id)
        {
            using (var db = new QDMallDatabase())
            {
                db.Delete<DiningTableType>(id);
            }
        }
        public static void SetCurrDiscount(int id,decimal discount)
        {
            if (discount <= 0) return;
            using (var db = new QDMallDatabase())
            {
                db.Execute($"update bma_DiningTable set currdiscount={discount} where id={id}");
            }
        }
        public static int GetIdFromTableNumber(int storeId,string tableNumber)
        {
            using (var db = new QDMallDatabase())
            {
                return db.ExecuteScalar<int>("select id from bma_DiningTable where storeid=@0 and tablenumber=@1",
                    storeId, tableNumber);
            }
        }
        public static decimal GetCurrDiscount(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.ExecuteScalar<decimal>($"select currdiscount from bma_DiningTable where id={id}");
            }
        }
        public static void ClearCurrDiscount(int id)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute($"update bma_DiningTable set currdiscount=0 where id={id}");
            }
        }
        public static DiningTableType GeTableType(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<DiningTableType>(id);
            }
        }

        public static bool CheckTableTypeUsing(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.ExecuteScalar<int>("select count(*) from bma_DiningTable where TableTypeId=@0", id) > 0;
            }
        }

        public static IList<DiningTableType> GetStoreDiningTableTypes(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<DiningTableType>("select * from bma_diningtabletype where storeid=@0", storeId);
            }
        }

        public static void AddDiningTable(DiningTable diningTable)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(diningTable);
            }
        }

        public static void EditDiningTable(DiningTable diningTable)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(diningTable);
            }
        }

        public static DiningTable GetDiningTable(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<DiningTable>(id);
            }
        }

        public static void DeleteDiningTable(int id)
        {
            using (var db = new QDMallDatabase())
            {
                db.Delete<DiningTable>(id);
            }
            
        }

        public static IList<DiningTable> GetStoreDiningTables(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<DiningTable>("select * from bma_DiningTable where storeid=@0 order by TableTypeId", storeId);
            }
        }
        

    }
}
